<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of message
 *
 * @author 10520_000
 */
class message extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('connect');
        $this->load->helper("url");
        $this->load->helper("date");
        $this->load->database();
    }

    function save() {


        $c = $this->connect->checkconversation($this->session->userdata('id'), $_POST['user-id']);
        $d = $this->connect->countconversation();
        foreach ($c as $i) {
            $e = $i->conversation_id;
        }
        if ($c != NULL) {


             $c = $this->connect->getlastmessage($b);
            foreach ($c as $k) {
                $p = $k->message_id;
            }

            $now = date("Y-m-d H:i:s");
            $data1 = array(
                'sender_id' => $this->session->userdata('id'),
                'receiver_id' => $_POST['user-id'],
                'content' => $_POST['content'],
                'date' => $now,
                'conversation_id' => $e,
                'status' => 'Chưa xem'
            );
            $this->db->insert('message', $data1);

            $data3 = array(
                'status' => 'Đã xem'
            );

            $this->db->where('message_id', $p);
            $this->db->update('message', $data3);
        } else {
            $now = date("Y-m-d H:i:s");
            $data = array(
                'sender_id' => $this->session->userdata('id'),
                'receiver_id' => $_POST['user-id'],
                'content' => $_POST['content'],
                'date' => $now,
                'conversation_id' => $d + 1,
                'status' => 'Chưa xem'
            );
            $this->db->insert('message', $data);
            $data2 = array(
                'user_id1' => $this->session->userdata('id'),
                'user_id2' => $_POST['user-id'],
                'conversation_id' => $d + 1,
            );
            $this->db->insert('conversations', $data2);
        }
        // echo $e;
    }

    function checkmessage() {
        $a = $this->connect->countmessage($this->session->userdata('id'));
        if ($a > 0) {
            echo $a;
        } else {
            echo '';
        }
    }

    function getconversations() {
        $a = $this->connect->getconversation($this->session->userdata('id'));
        foreach ($a as $i) {
            $b = $i->conversation_id;



            $c = $this->connect->getlastmessage($b);
            foreach ($c as $k) {
                $d = $k->message_id;
            }

            $e = $this->connect->getmessagedetails($d);
            foreach ($e as $l) {
                $f = $l->sender_id;
                $g = $l->receiver_id;
                $time = $l->date;
                $content = $l->content;
                $stt = $l->status;
            }
            

            if ($f == $this->session->userdata('id')) {
                $h = $this->connect->getID($g);
                $o = $g;
                foreach ($h as $m) {
                    $anh = $m->avarta;
                    $ten = $m->name;
                }
            } else {

                $j = $this->connect->getID($f);
                $o = $f;
                foreach ($j as $m) {
                    $anh = $m->avarta;
                    $ten = $m->name;
                }
            }
            if ($f != $this->session->userdata('id')&& $stt =='Chưa xem') {
                echo "<a id='bb' onclick='myStopFunction();updatemessage(" . $o . ");updatestt(" . $o . ");' class=' list-group-item'>";
            } else {
                echo "<a id='aa' onclick='myStopFunction();updatemessage(" . $o . ");updatestt(" . $o . ");' class=' list-group-item'>";
            }
            // echo "<input type='hidden' value='".$o."' id='id-conversation'/>";
            echo "<div class='anhdaidien'>";
            echo "<img class= 'anh img-thumbnail' src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";
            echo "</div>";
            echo "<div class ='ten'>";
            echo "<p style='font-size: 12px;' ><b>" . $ten . "</b></p>";
            echo "</div>";
            echo "</a>";
        }
    }

    function getmessage() {
        $a = $this->connect->checkconversation($this->session->userdata('id'), $_POST['id']);
        foreach ($a as $i) {
            $b = $i->conversation_id;


            $c = $this->connect->getmessage($b);
            foreach ($c as $k) {
                $d = $k->message_id;


                $e = $this->connect->getmessagedetails($d);
                foreach ($e as $l) {
                    $f = $l->sender_id;

                    $time = $l->date;
                    $content = $l->content;
                }


                $h = $this->connect->getID($f);
                foreach ($h as $m) {
                    $anh = $m->avarta;
                    $ten = $m->name;
                }


                echo "<li class='tinnhan'>";
                echo "<div class='anhdaidien2'>";
                echo "<img class= 'anhnho img-thumbnail' src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";
                echo "</div>";
                echo "<div class ='ten2'>";
                echo "<p style='color: #003399;'>" . $ten . "</p>";
                echo "<p style='font-size: 11.5px;'>" . $content . "</p>";
                echo "</div>";
                echo "<div class ='thoigian'>";
                echo "<p>" . $time . "</p>";
                echo "</div>";
                //echo "<p>".$b."</p>"; 
                echo "</liv>";
            }
        }
        foreach ($a as $i) {
            $b = $i->conversation_id;


            $c = $this->connect->getlastmessage($b);
            foreach ($c as $k) {
                $d = $k->message_id;
            }
            $e = $this->connect->checkownmessage($d);
            foreach ($e as $l) {
                $f = $l->sender_id;
            }
            if ($f == $this->session->userdata('id')) {
                echo "";
            } else {
                echo "<p id='typing'></p>";
            }
        }
    }

    function chatbox() {
        $c = $this->connect->checkconversation($this->session->userdata('id'), $_POST['id']);

        foreach ($c as $i) {
            $e = $i->conversation_id;
        }

        $now = date("Y-m-d H:i:s");
        $data1 = array(
            'sender_id' => $this->session->userdata('id'),
            'receiver_id' => $_POST['id'],
            'content' => $_POST['chat'],
            'date' => $now,
            'conversation_id' => $e,
            'status' => 'Chưa xem'
        );
        $this->db->insert('message', $data1);

        $this->getmessage();
    }

    function updatestt() {
        $a = $this->connect->checkconversation($this->session->userdata('id'), $_POST['id']);
        foreach ($a as $i) {
            $b = $i->conversation_id;
            $c = $this->connect->getlastmessage($b);
            foreach ($c as $k) {
                $d = $k->message_id;
            }
            $data3 = array(
                'status' => 'Đã xem',
            );
            $this->db->where('message_id',$d);
            $this->db->update('message', $data3);
        }
        echo $d;
    }

    function typing() {
        echo $this->session->userdata('name') . " đang đánh bàn phim";
    }

}

?>
